# -*- coding: utf-8 -*-
# @Author  : gaoyu
# @Time    : 2025/6/14 16:00
# @Function:
# 字符串时间转换为毫秒时间戳的函数
from datetime import datetime, timezone, timedelta

import pytz


def datetime_str_to_timestamp_ms(datetime_str):
    """
    将字符串时间转换为毫秒时间戳

    参数:
        datetime_str (str): 时间字符串，格式如 "2025-06-12 23:00:00"

    返回:
        int: 毫秒时间戳
    """
    dt = datetime.strptime(datetime_str, "%Y-%m-%d %H:%M:%S")
    # 将时间设置为UTC+8时区
    utc8_tz = pytz.timezone('Asia/Shanghai')
    dt = utc8_tz.localize(dt)
    return int(dt.timestamp() * 1000)


def datetime_str_to_timestamp(datetime_str):
    """
    将字符串时间转换为毫秒时间戳

    参数:
        datetime_str (str): 时间字符串，格式如 "2025-06-12 23:00:00"

    返回:
        int: 毫秒时间戳
    """
    dt = datetime.strptime(datetime_str, "%Y-%m-%d %H:%M:%S")
    # 将时间设置为UTC+8时区
    utc8_tz = pytz.timezone('Asia/Shanghai')
    dt = utc8_tz.localize(dt)
    return int(dt.timestamp())


def timestamp_to_str(timestamp, format='%Y-%m-%d %H:%M:%S'):
    # 转换为秒级时间戳
    timestamp_seconds = timestamp / 1000
    # 创建UTC+8时区
    utc8_timezone = timezone(timedelta(hours=8))
    # 转换为datetime对象
    dt = datetime.fromtimestamp(timestamp_seconds, tz=utc8_timezone)
    # 格式化为字符串
    start_time_str = dt.strftime(format)
    return start_time_str
